package com.lhkj.ct.meta.modules.patient.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhkj.ct.base.annntation.DataPermission;
import com.lhkj.ct.base.model.UserAuthInfo;
import com.lhkj.ct.meta.modules.patient.model.dto.PatBasicInfo;
import com.lhkj.ct.meta.modules.patient.model.dto.PatientQuery;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatCrowdTag;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatUnit;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatient;
import com.lhkj.ct.meta.modules.patient.model.vo.PatientBasicVo;
import com.lhkj.ct.meta.modules.patient.model.vo.PatientForm;
import com.lhkj.ct.meta.modules.patient.model.vo.PatientPageVo;
import com.lhkj.ct.meta.modules.psychology.model.bo.SubmitGaugeBo;
import org.apache.ibatis.annotations.*;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 档案表 Mapper 接口
 * </p>
 */
public interface PatientMapper extends BaseMapper<TblPatient> {

    @InterceptorIgnore
    int insertOnDuplicate(TblPatient patient);

    @Select("select ${ew.sqlSelect}, fun_convert_dictionary(pat_sex, '${@com.lhkj.ct.base.constants.DictConstants@GENDER}') as patSexName " +
            "from tbl_patient ${ew.customSqlSegment}")
    PatientForm getForm(@Param(Constants.WRAPPER) Wrapper<TblPatient> wrapper);

    /**
     * 根据账号查询登录信息
     */
    @Select("select id as `user_id`, account as username, pat_name as `name`, `password`, `status` " +
            "from tbl_patient " +
            "where account = #{username,jdbcType=VARCHAR}")
    UserAuthInfo getUserAuthInfo(String username);

    @DataPermission(orgAlias = "t", orgIdColumnName = "pat_organ")
    IPage<PatientPageVo> getUserPage(Page<TblPatient> page, @Param("queryParams") PatientQuery patientQuery);

    IPage<PatientBasicVo> getBasicUserPage(Page<TblPatient> page, @Param("queryParams") PatientQuery patientQuery);

    Set<String> selectUsersByUnits(@Param("unitIds") Collection<?> unitIds);

    Set<String> selectUsersByLabels(@Param("labelIds") Collection<?> labelIds);

    /**
     * 树级列表包含所包含档案数量
     *
     * @return
     */
    List<TblPatUnit> selectTreeWithPatCount(@Param("unit") TblPatUnit unit);

    /**
     * 按标签分组统计
     *
     * @param unit
     * @return
     */
    List<TblPatCrowdTag> selectPatTagWithCount(@Param("tag") TblPatCrowdTag unit);

    /**
     * 获取提交作答时需要用到的用户信息，id，性别，年龄
     * @param patId
     * @return
     */
    SubmitGaugeBo.Patient getSubmitUserInfo(@Param("patId") String patId);

    TblPatient getReportUserInfo(@Param("patId") String patId);

    @Select("select t.id, " +
            "t.pat_name, " +
            "t.account, " +
            "t.pat_sex, " +
            "t.pat_ident_type, " +
            "t.pat_ident_no, " +
            "t.pat_birthday, " +
            "t.pat_nation, " +
            "t.pat_address, " +
            "t.pat_email, " +
            "t.pat_mobile, " +
            "t.pat_avatar," +
            "t2.organ_name as pat_organ, " +
            "2 as user_type  " +
            "from tbl_patient t " +
            "left join tbl_organ t2 on t2.id = t.pat_organ " +
            "where t.id = #{patId,jdbcType=VARCHAR}")
    @Results(id = "getBasicInfoMap", value = {
            @Result(property = "identities", column = "{id = id, user_type = user_type}", javaType = List.class,
                    many = @Many(select = "com.lhkj.ct.meta.modules.auth.mapper.UserIdentityMapper.selectByUserId"))
    })
    PatBasicInfo getBasicInfo(@Param("patId") String patId);
}
